package com.xxy.controller;

import com.xxy.common.Result;
import com.xxy.pojo.Category;
import com.xxy.services.CategoryService;
import com.xxy.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author xingyu Xue
 * @Date 2024/2/6 20:47
 * 文章分类相关接口
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * > 接口描述：该接口用于新增文章分类
     * 新增接口，应该要求前端提交 创建人id，创建时间，更新时间，等信息，应添加参数校验
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        String category_name = category.getCategory_name();
        String category_alias = category.getCategory_alias();

        categoryService.add(category_name,category_alias);
        return  Result.success();

    }

    /**
     * > 接口描述：该接口用于获取当前已登录用户创建的所有文章分类
     *
     * ####
     * @return
     */
    @GetMapping
    public Result<List<Category>> getCategory(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        List<Category> category = categoryService.getCategory(id);
        return Result.success(category);

    }

    /**
     * > 接口描述：该接口用于根据ID获取文章分类详情
     *
     * ####
     * @return
     */
    @GetMapping("/detail")
    public Result detail(Integer id){
        Category byId = categoryService.findById(id);
        return Result.success(byId);
    }

    /**
     * > 接口描述：该接口用于更新文章分类
     *
     * ####
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        Integer id = category.getId();
        String category_name = category.getCategory_name();
        String category_alias = category.getCategory_alias();
        log.info("category_name:{}",category_name);
        log.info("category_alias:{}",category_alias);
        log.info("id:{}",id);

        categoryService.update(category);
        return Result.success();
    }

    /**
     * > 接口描述：该接口用于根据ID删除文章分类
     *
     * ####
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        log.info("id:{}",id);

        categoryService.delete(id);


        return Result.success();
    }

}
